package com.Data;

import java.io.Serializable;
import java.util.LinkedList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.SmileBreaker.DisplayInfo;
import com.SmileBreaker.R;

public class GameStage implements Serializable, Storable {
	private static final long serialVersionUID = 5358501821625872496L;
	
	public final static int DEFAULT = 0;
	public final static int WOOD = 0;
	public final static int STONES = 1;
	public final static int TILES = 2;
	public final static int SKY = 3;
	public final static int LAST_STAGE = 4;
	
	
	private transient Resources res;
	private int stageNumber;
	private LinkedList<Block> blocks;
	private LinkedList<Trap> traps;
	private int typeBackground = DEFAULT;
	private transient Bitmap background;
	
	public GameStage(Context context, int type) {
		res = context.getResources();
		stageNumber = 0;
		blocks = new LinkedList<Block>();
		traps = new LinkedList<Trap>();
		setBackground(type);
	}
	
	public boolean save(int stageNum) {
		return FileManager.save(stageNum, this, FileManager.GAMESTAGE);
	}
	
	public boolean load(int stageNum) {
		return FileManager.load(stageNum, this, FileManager.GAMESTAGE);
	}

	@Override
	public void setData(Storable data) {
		GameStage gameStage = (GameStage)data;
		
		this.stageNumber = gameStage.stageNumber;
		this.blocks = gameStage.blocks;
		this.traps = gameStage.traps;
		this.typeBackground = gameStage.typeBackground;
		setBackground(typeBackground);
		
		for (int i = 0; i < blocks.size(); i++) {
			blocks.get(i).setContext(res);
			blocks.get(i).setData(gameStage.blocks.get(i));
		}
		
		for (int i = 0; i < traps.size(); i++) {
			traps.get(i).setContext(res);
			traps.get(i).setData(gameStage.traps.get(i));
		}
	}
	
	public int getStageNumber() {	return stageNumber;	}
	public void setStageNumber(int stageNumber) {	this.stageNumber = stageNumber;	}
	public LinkedList<Block> getBlocks() {	return blocks;	}
	public void setBlocks(LinkedList<Block> blocks) {	this.blocks = blocks;	}
	public LinkedList<Trap> getTraps() {	return traps;	}
	public void setTraps(LinkedList<Trap> traps) {	this.traps = traps;	}
	public Bitmap getBackground() {	return background;	}
	public int getTypeBackground() {	return typeBackground;	}
	public void setBackground(int type) {
    	BitmapFactory.Options option = new BitmapFactory.Options();
    	option.inSampleSize = 1;
    	option.inPurgeable = true;
    	option.inDither = true;
    	
    	if(background != null) background.recycle();
    	
		typeBackground = type;
		if(typeBackground == WOOD) {
			background = BitmapFactory.decodeResource(res, R.drawable.background_wood, option);
			background = Bitmap.createScaledBitmap(background,
												   DisplayInfo.LOGICAL_WIDTH-DisplayInfo.SPACE_WIDTH*2,
												   DisplayInfo.LOGICAL_HEIGHT, false);
		} else if(typeBackground == STONES) {
			background = BitmapFactory.decodeResource(res, R.drawable.background_stones, option);
			background = Bitmap.createScaledBitmap(background,
												   DisplayInfo.LOGICAL_WIDTH-DisplayInfo.SPACE_WIDTH*2,
												   DisplayInfo.LOGICAL_HEIGHT, false);
		} else if(typeBackground == TILES) {
			background = BitmapFactory.decodeResource(res, R.drawable.background_tiles, option);
			background = Bitmap.createScaledBitmap(background,
												   DisplayInfo.LOGICAL_WIDTH-DisplayInfo.SPACE_WIDTH*2,
												   DisplayInfo.LOGICAL_HEIGHT, false);
		} else if(typeBackground == SKY) {
			background = BitmapFactory.decodeResource(res, R.drawable.background_sky, option);
			background = Bitmap.createScaledBitmap(background,
												   DisplayInfo.LOGICAL_WIDTH-DisplayInfo.SPACE_WIDTH*2,
												   DisplayInfo.LOGICAL_HEIGHT, false);
		} else if(typeBackground == LAST_STAGE) {
			background = BitmapFactory.decodeResource(res, R.drawable.background_last_stage, option);
			background = Bitmap.createScaledBitmap(background,
												   DisplayInfo.LOGICAL_WIDTH-DisplayInfo.SPACE_WIDTH*2,
												   DisplayInfo.LOGICAL_HEIGHT, false);
		} else {
			typeBackground = DEFAULT;
			background = BitmapFactory.decodeResource(res, R.drawable.background_wood, option);
			background = Bitmap.createScaledBitmap(background,
												   DisplayInfo.LOGICAL_WIDTH-DisplayInfo.SPACE_WIDTH*2,
												   DisplayInfo.LOGICAL_HEIGHT, false);
		}
	}
}
